stringFuncs.restrictToKeywords   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
/** global: UB */
2
3
UB.stopWords = ["a", "a’s", "able", "about", "above", "according", "accordingly", "across", "actually", "after", "afterwards", "again", "against", "ain’t", "ain't", "all", "allow", "allows", "almost", "alone", "along", "already", "also", "although", "always", "am", "among", "amongst", "an", "and", "another", "any", "anybody", "anyhow", "anyone", "anything", "anyway", "anyways", "anywhere", "apart", "appear", "appreciate", "appropriate", "are", "aren’t", "aren't", "around", "as", "a's", "aside", "ask", "asking", "associated", "at", "available", "away", "awfully", "be", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", "behind", "being", "believe", "below", "beside", "besides", "best", "better", "between", "beyond", "both", "brief", "but", "by", "c’mon", "c’s", "came", "can", "can’t", "cannot", "cant", "can't", "cause", "causes", "certain", "certainly", "changes", "clearly", "c'mon", "co", "com", "come", "comes", "concerning", "consequently", "consider", "considering", "contain", "containing", "contains", "corresponding", "could", "couldn’t", "couldn't", "course", "c's", "currently", "definitely", "described", "despite", "did", "didn’t", "didn't", "different", "do", "does", "doesn’t", "doesn't", "doing", "don’t", "done", "don't", "down", "downwards", "during", "each", "edu", "eg", "eight", "either", "else", "elsewhere", "enough", "entirely", "especially", "et", "etc", "even", "ever", "every", "everybody", "everyone", "everything", "everywhere", "ex", "exactly", "example", "except", "far", "few", "fifth", "first", "five", "followed", "following", "follows", "for", "former", "formerly", "forth", "four", "from", "further", "furthermore", "get", "gets", "getting", "given", "gives", "go", "goes", "going", "gone", "got", "gotten", "greetings", "had", "hadn’t", "hadn't", "happens", "hardly", "has", "hasn’t", "hasn't", "have", "haven’t", "haven't", "having", "he", "he’s", "hello", "help", "hence", "her", "here", "here’s", "hereafter", "hereby", "herein", "here's", "hereupon", "hers", "herself", "he's", "hi", "him", "himself", "his", "hither", "hopefully", "how", "howbeit", "however", "i’d", "i’ll", "i’m", "i’ve", "i'd", "ie", "if", "ignored", "i'll", "i'm", "immediate", "in", "inasmuch", "inc", "indeed", "indicate", "indicated", "indicates", "inner", "insofar", "instead", "into", "inward", "is", "isn’t", "isn't", "it", "it’d", "it’ll", "it’s", "it'd", "it'll", "its", "it's", "itself", "i've", "just", "keep", "keeps", "kept", "know", "known", "knows", "last", "lately", "later", "latter", "latterly", "least", "less", "lest", "let", "let’s", "let's", "like", "liked", "likely", "little", "look", "looking", "looks", "ltd", "mainly", "many", "may", "maybe", "me", "mean", "meanwhile", "merely", "might", "more", "moreover", "most", "mostly", "much", "must", "my", "myself", "name", "namely", "nd", "near", "nearly", "necessary", "need", "needs", "neither", "never", "nevertheless", "new", "next", "nine", "no", "nobody", "non", "none", "noone", "nor", "normally", "not", "nothing", "novel", "now", "nowhere", "obviously", "of", "off", "often", "oh", "ok", "okay", "old", "on", "once", "one", "ones", "only", "onto", "or", "other", "others", "otherwise", "ought", "our", "ours", "ourselves", "out", "outside", "over", "overall", "own", "particular", "particularly", "per", "perhaps", "placed", "please", "plus", "possible", "presumably", "probably", "provides", "que", "quite", "qv", "rather", "rd", "re", "really", "reasonably", "regarding", "regardless", "regards", "relatively", "respectively", "right", "said", "same", "saw", "say", "saying", "says", "second", "secondly", "see", "seeing", "seem", "seemed", "seeming", "seems", "seen", "self", "selves", "sensible", "sent", "serious", "seriously", "seven", "several", "shall", "she", "should", "shouldn’t", "shouldn't", "since", "six", "so", "some", "somebody", "somehow", "someone", "something", "sometime", "sometimes", "somewhat", "somewhere", "soon", "sorry", "specified", "specify", "specifying", "still", "sub", "such", "sup", "sure", "t’s", "take", "taken", "tell", "tends", "th", "than", "thank", "thanks", "thanx", "that", "that’s", "thats", "that's", "the", "their", "theirs", "them", "themselves", "then", "thence", "there", "there’s", "thereafter", "thereby", "therefore", "therein", "theres", "there's", "thereupon", "these", "they", "they’d", "they’ll", "they’re", "they’ve", "they'd", "they'll", "they're", "they've", "think", "third", "this", "thorough", "thoroughly", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "took", "toward", "towards", "tried", "tries", "truly", "try", "trying", "t's", "twice", "two", "un", "under", "unfortunately", "unless", "unlikely", "until", "unto", "up", "upon", "us", "use", "used", "useful", "uses", "using", "usually", "value", "various", "very", "via", "viz", "vs", "want", "wants", "was", "wasn’t", "wasn't", "way", "we", "we’d", "we’ll", "we’re", "we’ve", "we'd", "welcome", "well", "we'll", "went", "were", "we're", "weren’t", "weren't", "we've", "what", "what’s", "whatever", "what's", "when", "whence", "whenever", "where", "where’s", "whereafter", "whereas", "whereby", "wherein", "where's", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "who’s", "whoever", "whole", "whom", "who's", "whose", "why", "will", "willing", "wish", "with", "within", "without", "won’t", "wonder", "won't", "would", "would", "wouldn’t", "wouldn't", "yes", "yet", "you", "you’d", "you’ll", "you’re", "you’ve", "you'd", "you'll", "your", "you're", "yours", "yourself", "yourselves", "you've", "zero", "represents", "represent", "represented", "e.g.", "eg.", "important", "unimportant"];
4
5
var stringFuncs = {
6
	
7
	/**
8
	*	Returns a string truncated to a specified length with optional suffix
9
	*
10
	*	@param len The length the string should be shortend to
11
	*
12
	*	@param suffix (optional, default=...) The string to append to the end of the truncated string.
13
	*/
14
	truncate: function(len, suffix = "..."){
15
		var text = this;
16
		len -= suffix.length;
17
		var trunc = text;
18
		if (trunc.length > len) {
19
			trunc = trunc.substr(0, len);
20
			if (new RegExp("[^\\s]", "").test(text.charAt(len))) {
21
				trunc = trunc.replace(new RegExp("\\w+$|\\s+$", ""), '').trimRight();
22
			}
23
			trunc += suffix;
24
		}
25
	
26
		return trunc;
27
	},
28
	
29
	restrictToLettersDigits: function(spacesOk = false){
30
		var str = this;
31
		/* INPUT 		~!@#$%&* {:'\"<>[ © € Characters 2007
32
		 * OUTPUT		Characters2007								*/
33
		if (str == null) {
34
			return null;
35
		}
36
		var result = [];
37
		for (var a = 0;a<str.length;a++){
38
			var char = str.charCodeAt(a);
39
			if ((char >= UB.charCodes._0 && char <= UB.charCodes._9) ||
40
				(char >= UB.charCodes.A && char <= UB.charCodes.Z) ||
41
				(char >= UB.charCodes.a && char <= UB.charCodes.z) ||
42
				(spacesOk && char == UB.charCodes.Space)) {
43
				result.push(str.charAt(a));
44
			}
45
		}
46
		return result.join("");
47
	}, 
48
	restrictToLetters: function(spacesOk = false){
49
		var str = this;
50
		/* INPUT 		~!@#$%&* {:'\"<>[ © € Characters 2007
51
		 * OUTPUT		Characters								*/
52
		if (str == null) {
53
			return null;
54
		}
55
		var result = [];
56
		for (var a = 0;a<str.length;a++){
57
			var char = str.charCodeAt(a);
58
			if ((char >= UB.charCodes.A && char <= UB.charCodes.Z) ||
59
				(char >= UB.charCodes.a && char <= UB.charCodes.z) ||
60
				(spacesOk && char == UB.charCodes.Space)) {
61
				result.push(str.charAt(a));
62
			}
63
		}
64
		return result.join("");
65
	}, 
66
	restrictToDigits: function(spacesOk = false){
67
		var str = this;
68
		/* INPUT 		~!@#$%&* {:'\"<>[ © € Characters 2007
69
		 * OUTPUT		2007								*/
70
		if (str == null) {
71
			return null;
72
		}
73
		var result = [];
74
		for (var a = 0;a<str.length;a++){
75
			var char = str.charCodeAt(a);
76
			if ((char >= UB.charCodes._0 && char <= UB.charCodes._9) ||
77
				(spacesOk && char == UB.charCodes.Space)) {
78
				result.push(str.charAt(a));
79
			}
80
		}
81
		return result.join("");
82
	},
83
	restrictToFilename: function(replaceWith = "_"){
84
		var path = this;
85
		return path.ensureValidFilename(replaceWith);
86
	},
87
	restrictToIdentifier: function(useCamelCase = false, firstCharCaps = false, keepSymbols = false){
88
		var str = this;
89
		/* INPUT 		~!@#$%&* {:'\"<>[ © € my characters 2007
90
		 * OUTPUT		MyCharacters2007				*/
91
		
92
		 if (keepSymbols) {
93
			str = str.flattenSymbols();
94
		 }
95
		 
96
		var parts = [];
97
		for (var a = 0;a<str.length;a++){
98
			var c = str.charAt(a);
99
			if (c.isAlphaNumeric() || (useCamelCase && c == " ") || c == "_"){
100
				parts.push(c);
101
			}
102
		}
103
		var result = parts.join("");
104
		if (result.charAt(0).isDigit()) {
105
			result = "_" + result;
106
		}
107
		return useCamelCase ? result.camelCaseMerge(firstCharCaps) : result;
108
	}, 
109
110
	restrictToKeywords: function(){
111
		var text = this;
112
		
113
		text = text.replaceManyWithOne(UB.stopWords, ".", true);
114
		
115
		var keywords = text.splitWords(true);
116
		
117
		return keywords;
118
	},
119
120
121
	none:null
122
};
123
124
// register funcs
125
UB.registerFuncs(String.prototype, stringFuncs);